package com.skzh.equipment.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.skzh.common.exception.ServiceException;
import com.skzh.common.utils.DateUtils;
import com.skzh.equipment.domain.DockConfig;
import com.skzh.equipment.domain.Drone;
import com.skzh.equipment.mapper.DockConfigMapper;
import com.skzh.equipment.mapper.DroneMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.skzh.equipment.mapper.DockMapper;
import com.skzh.equipment.domain.Dock;
import com.skzh.equipment.service.IDockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 机场基本信息Service业务层处理
 *
 * @author skzh
 * @date 2025-03-21
 */
@Service
public class DockServiceImpl implements IDockService {
    @Autowired
    private DockMapper dockMapper;
    @Autowired
    private DockConfigMapper dockConfigMapper;
    @Autowired
    private DroneServiceImpl droneServiceImpl;
    @Autowired
    private DroneMapper droneMapper;

    /**
     * 查询机场基本信息
     *
     * @param id 机场基本信息主键
     * @return 机场基本信息
     */
    @Override
    public Dock selectDockById(Long id) {
        return dockMapper.selectDockById(id);
    }

    /**
     * 查询机场基本信息列表
     *
     * @param dock 机场基本信息
     * @return 机场基本信息
     */
    @Override
    public List<Dock> selectDockList(Dock dock) {
        return dockMapper.selectDockList(dock);
    }

    /**
     * 新增机场基本信息
     *
     * @param dock 机场基本信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertDock(Dock dock) {
        List<String> searchValues = dockMapper.selectAllSearchValue();
        if (searchValues.contains(dock.getSerialNumber())){
            throw new ServiceException("设备序列号已存在");
        }
        int i = dockMapper.insertDock(dock);
        dock.setCreateTime(DateUtils.getNowDate());
        DockConfig dockConfig = new DockConfig();
        dockConfig.setDockId(dock.getId());
        dockConfig.setWorkRange(7.0);
        dockConfig.setReturnWindSpeed(15.0);
        dockConfig.setReturnRainfall("0");
        dockConfig.setStreamProtocol("0");
        dockConfig.setCreateBy(dock.getCreateBy());
        dockConfig.setCreateTime(dock.getCreateTime());
        dockConfigMapper.insertDockConfig(dockConfig);
        return i;
    }

    /**
     * 修改机场基本信息
     *
     * @param dock 机场基本信息
     * @return 结果
     */
    @Override
    public int updateDock(Dock dock) {
        dock.setUpdateTime(DateUtils.getNowDate());
        return dockMapper.updateDock(dock);
    }

    /**
     * 批量删除机场基本信息
     *
     * @param ids 需要删除的机场基本信息主键
     * @return 结果
     */
    @Override
    public int deleteDockByIds(Long[] ids) {
        return dockMapper.deleteDockByIds(ids);
    }

    /**
     * 删除机场基本信息信息
     *
     * @param id 机场基本信息主键
     * @return 结果
     */
    @Override
    public int deleteDockById(Long id) {
        return dockMapper.deleteDockById(id);
    }

    @Override
    public List<Map<String, Object>> selectAllNoBind() {
        List<Map<String, Object>> maps = dockMapper.selectAllNoBind();
        List<Map<String, Object>> maps1 = selectAllDrone();
        maps.removeIf(map ->
                maps1.stream().anyMatch(m -> m.get("bound_airport_id").equals(map.get("id")))
        );
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectAllDrone() {
        return droneMapper.selectAllDrone();
    }

    /**
     * 查询在线设备列表(包括机场和无人机)
     *
     * @param dock 查询条件
     * @return 在线设备信息集合
     */
    @Override
    public List<Map<String, Object>> selectOnlineDevicesList(Dock dock) {
        return dockMapper.selectOnlineDevicesList(dock);
    }
}
